import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
//存放组件之间共享的数据
const state = {
  token: '467546rtyfytftyfy',
  name: 'jack'
}
//过滤state数据
const getters = {
  getname: state => {
    return state.name;
  },
  // 将getters作为自己的参数，调用其他方法返回结果
  getnamelen: (state, getters) => {
    return getters.getname.length;
  }
}
//显式的更改state里的数据，同步事务
const mutations = {
  updateName(state) {
    state.name = 'tom';
  },
  updateToken(state, value) {
    state.token = value;
  }
}
//可执行异步操作
const actions = {
  //此处的storeObj可以理解成与store有相同功能和属性的对象
  actionsToken(storeObj) {
    storeObj.commit('updateToken', '6666');
  },
  actionsName({
    commit
  }) {
    commit('updateToken', '6666');
  },
  actionLoginOut(storeObj) {
    storeObj.commit('updateToken', '');
    localStorage.removeItem('authenticationtoken');
  },

}
//模块化
// const  storeModule={
//     state,
//     getters,
//     mutations,
//     actions
// }
// var store=new Vuex.Store();
// store.modules={
//     storeModule
// }
var store = new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  modules: {},
});

export default store;
